package com.qf.springmvc.controller;

import com.qf.springmvc.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author 钻五大人
 * @date 2022/4/29 17:30
 */
@Controller
@RequestMapping("data")
@SessionAttributes({"name","age"})
// model中的name和age会存入session中
public class DataController {

    // 原始的传值方式
    @RequestMapping("test1")
    public String test1(HttpServletRequest request, HttpSession session){
        // 到request域中存数据
        request.setAttribute("name","钻五");
        request.setAttribute("age",23);
        session.setAttribute("date","2020-2-20");
        return  "welcome";
    }

    // Spring推荐的传值方式
    @RequestMapping("test2")
    public String test2(Model model){
        // model中的数据是放到request中的
        model.addAttribute("name","霏霏");
        model.addAttribute("age",18);
        // 如果没有指定key的话,key默认就是value值的类型
        model.addAttribute("紫辉");
        User user = new User();
        user.setName("钻五大人");
        user.setPassword("123456");
        model.addAttribute(user);
        return "welcome";
    }

    @RequestMapping("test3")
    public ModelAndView test3(){
        ModelAndView modelAndView = new ModelAndView("welcome");
        modelAndView.addObject("name","艾瑞利亚");
        return modelAndView;
    }
}
